// pages/Order/ServiceOrderDetail/ServiceOrderDetail.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetServiceOrderForm , delPetServiceOrderForm , updatePetServiceOrderStatus } from '../../../api/petOrder';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    orderCode: '',
    order: {},
    safaBtm: wx.getSystemInfoSync().screenHeight - wx.getSystemInfoSync().safeArea.bottom,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.orderCode){
      this.setData({
        orderCode: options.orderCode,
      })
      this._queryPetServiceOrderForm();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this._queryPetServiceOrderForm()
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  _queryPetServiceOrderForm(){
    let _this = this;
    queryPetServiceOrderForm({orderCode: _this.data.orderCode,page: 0,size: 1})
    .then(res => {
      if(res.success){
        var order = res.data.content.length > 0 ? res.data.content[0] : {};
        _this.setData({
          order: order,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 删除订单
  delOrder(){
    let _this = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否删除当前订单？',
      complete: (res) => {
        if (res.confirm) {
          delPetServiceOrderForm({orderCode: _this.data.orderCode,})
          .then(res => {
            if(res.success){
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              });
              const eventChannel = _this.getOpenerEventChannel()
              eventChannel.emit('updateOrder',);
              wx.navigateBack();
            }else{
              wx.showToast({
                title: res.errMessage,
                icon: 'none',
              })
            }
          })
        }
      }
    })
  },
  // 退款
  refundOrder(){
    let _this = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否取消预约当前服务项目？',
      complete: (res) => {
        if (res.confirm) {
          updatePetServiceOrderStatus({orderCode: _this.data.orderCode,orderStatus: '2'})
          .then(res => {
            if(res.success){
              wx.showToast({
                title: '取消成功',
                icon: 'success'
              });
              _this._queryPetServiceOrderForm();
              const eventChannel = _this.getOpenerEventChannel()
              eventChannel.emit('updateOrder',);
            }else{
              wx.showToast({
                title: res.errMessage,
                icon: 'none',
              })
            }
          })
        }
      }
    })
  },
})